Ext.define('Admin.view.leave.LeaveViewController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.leave',
    onClickLeaveGridAddButton: function (view) {
    		Ext.create(Ext.apply({xtype: 'leave-add-window'}));
    },
    onClickAddFormSubmitButton: function (btn) {
    	var form = btn.up('form');
		form.getForm().submit({       
			url:'leave/saveOrUpdate',
			method:'post',
			params: {
                "_csrf": document.getElementsByTagName('meta')['_csrf'].content
            },
			success: function(form, action){    
				Ext.Msg.alert('Success', action.result.msg);
				Ext.getCmp('leave-grid').getStore().reload();
				btn.up('window').close();
			},       
			failure: function(form, action){
				Ext.Msg.alert('Error', action.result.msg);
			}
		});
    },
	onClickLeaveGridUpdateButton: function (btn) {
      	var gridPanel = btn.up('gridpanel');
		var selected = gridPanel.getSelectionModel();
		if (selected.hasSelection()) {
			var record = selected.getSelection()[0];
			Ext.Ajax.request({
                url:'leave/isChangeable',
                method:'post',
                defaultHeaders: {
                    "X-CSRF-TOKEN": document.getElementsByTagName('meta')['_csrf'].content
                },
                params:{
                    processInstanceId:record.data.processInstanceId
                },
                success:function(response,options){
                    var json = Ext.util.JSON.decode(response.responseText)
                    if (!json.success) {
                        Ext.Msg.alert('提示', json.msg+',不能进行修改');
                    } else {
                        var record = selected.getSelection()[0];
						var win = Ext.create(Ext.apply({xtype: 'leave-add-window'}));
						win.down('form').getForm().loadRecord(record);
			            var begin = Ext.getCmp('leaveStartTime');
			            Ext.getCmp('leaveEndTime').setMinValue(begin.value);
                    }
                }
            });	
		}else {
            Ext.Msg.alert("错误", "没有任何行被选中，无法进行修改操作！");
        }
    },
	onClickLeaveGridDeleteButton: function (btn) {
		var grid = btn.up('gridpanel');
		var selModel = grid.getSelectionModel();
        if (selModel.hasSelection()) {
        	var record = selModel.getSelection()[0];
        	Ext.Ajax.request({
                url:'leave/isChangeable',
                method:'post',
                defaultHeaders: {
                    "X-CSRF-TOKEN": document.getElementsByTagName('meta')['_csrf'].content
                },
                params:{
                    processInstanceId:record.data.processInstanceId
                },
                success:function(response,options){
                    var json = Ext.util.JSON.decode(response.responseText)
                    if (!json.success) {
                        Ext.Msg.alert('提示', json.msg+',不能进行删除');
                    } else {
                        Ext.Msg.confirm("警告", "确定要删除吗？", function (button) {
			                if (button == "yes") {
			                    var selected = selModel.getSelection()[0];
			                  	Ext.Ajax.request({ 
									url : 'leave/delete', 
									method : 'post', 
									defaultHeaders: {
				                        "X-CSRF-TOKEN": document.getElementsByTagName('meta')['_csrf'].content
				                    },
									params : { 
										//ids[] :selectIds
										id :selected.data.id
									}, 
									success: function(response, options) {
										console.log(response);
						                var json = Ext.util.JSON.decode(response.responseText);
							            if(json.success){
							            	Ext.Msg.alert('操作成功', json.msg, function() {
							                    grid.getStore().reload();
							                });
								        }else{
								        	 Ext.Msg.alert('操作失败', json.msg);
								        }
						            }
								});
			                }
            			});
                    }
                }
            });
			            
        }
        else {
            Ext.Msg.alert("错误", "没有任何行被选中，无法进行删除操作！");
        }
    }
   
	,onClickStartButton: function (btn) {
		
		var gridPanel = btn.up('gridpanel');
		var selected = gridPanel.getSelectionModel();
		if (selected.hasSelection()) {		
			var record = gridPanel.getSelectionModel().getSelection()[0];
        	Ext.Ajax.request({
                url:'leave/isChangeable',
                method:'post',
                defaultHeaders: {
                    "X-CSRF-TOKEN": document.getElementsByTagName('meta')['_csrf'].content
                },
                params:{
                    processInstanceId:record.data.processInstanceId
                },
                success:function(response,options){
                    var json = Ext.util.JSON.decode(response.responseText)
                    if (!json.success) {
                        Ext.Msg.alert('提示', json.msg+',不能进行申请');
                    } else {
                        Ext.Ajax.request({ 
							url : 'leave/start', 
							method : 'post', 
							defaultHeaders: {
				                "X-CSRF-TOKEN": document.getElementsByTagName('meta')['_csrf'].content
				            },
							params : { 
								id :record.get('id')//record.data.id
							}, 
							success: function(response, options) {
				                var json = Ext.util.JSON.decode(response.responseText);
					            if(json.success){
					            	Ext.Msg.alert('操作成功', json.msg, function() {
					                    gridPanel.getStore().reload();
					                });
						        }else{
						        	 Ext.Msg.alert('操作失败', json.msg);
						        }
				            }
						});
                    }
                }
			});		
		}else{
			Ext.Msg.alert("错误", "没有任何行被选中，无法进行申请操作！");
		}
	}

	,onClickLeaveApproveCompleteWindowButton: function(view, recIndex, cellIndex, item, e, record) {
		var obj = this;
    	Ext.Ajax.request({
    		url:'leave/getTask.json',
    		method:'get',
    		params:{
    			processInstanceId:record.get('processInstanceId')
    		},
    		success:function(response){
    			var json = Ext.util.JSON.decode(response.responseText);
    			var taskDefinitionKey = json.taskDefinitionKey;
		        if (taskDefinitionKey == 'reportBack') {
		        	 //申请人销假
		        	obj.setCurrentView('leaveapprove_reportBack_form', '销假');
		            var form = Ext.getCmp('leaveapprove_reportBack_form').getForm();
		            form.findField('taskId').setValue(json.taskId);
			        form.findField('id').setValue(record.get('id'));
			        form.findField('taskDefinitionKey').setValue(taskDefinitionKey);
		        }
		        else if (taskDefinitionKey == 'modifyApply') {
		        	//申请人调整申请：可以编写到工具类中
		        	obj.setCurrentView('leaveapprove_modifyApply_form', '调整申请');
		            var form = Ext.getCmp('leaveapprove_modifyApply_form').getForm();
		            form.loadRecord(record);
		            form.findField('taskId').setValue(json.taskId);
			        form.findField('id').setValue(record.get('id'));
			        form.findField('taskDefinitionKey').setValue(taskDefinitionKey);
		        }
    		}
    	})    
    },

    setCurrentView: function(view, title) {
        if (view) {
            var cfg = Ext.apply({
                    xtype: 'leaveapprove-window',
                    items: [
                        Ext.apply({
                            xtype: view
                        }, '')
                    ]
                }, {
                    title: title
                });
            Ext.create(cfg);
        }
    }
    
});
